<template>
  <div>
    <el-dialog :title="title" v-model="dialogVisible" size="tiny" :close-on-click-modal="false" :before-close="closeDialog" v-on:open="loadData">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-form">
        <el-form-item label="父级字典">
          <el-select v-model="form.parentId" placeholder="选择父级字典">
            <el-option v-for="item in dictList" :label="item.name" :class="'lvl_'+item.lvl" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="字典名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="值" prop="val">
          <el-input v-model="form.val"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="active">
          <el-radio class="radio" v-model="form.active" :label="1">启用</el-radio>
          <el-radio class="radio" v-model="form.active" :label="0">禁用</el-radio>
        </el-form-item>
        <el-form-item class="button-row">
          <span style="color:red;float:left;">"*"为必填项。</span>
          <el-button type="primary" @click.native.prevent="handleSubmit">{{addBtnTxt}}</el-button>
          <el-button @click.native.prevent="handleReset">重置</el-button>
          <el-button @click.native.prevent="closeDialog">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  import http from '../../../api/http'
  var model = {name:null, active: 1,parentId:null,lvl:1,menuType:0,apiList:null};
  export default {
    data () {
      return {
        addBtnTxt:'',
        title: '字典添加', form: Object.assign({}, model),
        rules: {
          name: [
            {required: true, message: '请输入字典名称'},
            {min: 1, max: 20, message: '长度在 1 到 20 个字符'}
          ]
        }
      }
    },
    props: {
      dialogFormVisible: {
        type: Boolean,
        default: false
      },
      parent: {
        type: Object,
        default: {}
      }
    },
    computed: {
      dialogVisible: function () {
        var vm = this;
        vm.dictList = vm.$parent.list;
        return this.dialogFormVisible
      }
    },
    methods: {
      handleReset () {
        this.$refs.form.resetFields()
      },
      handleSubmit (ev) {
        var vm = this;
        this.$refs.form.validate(function (valid) {
          if (valid) {
            vm.form.dictTypeId = vm.$parent.form.dict_type_id;
            http.postData(vm,"/sys/dict/save", {json: JSON.stringify(vm.form)},function (resp) {
              var ret = resp.body;
              if (ret.code === '111') {
                vm.$notify({title: '操作提示', message: ret.msg, type: 'success'});
                vm.form = Object.assign({}, model);
                vm.closeDialog();
                vm.$parent.onSubmit();
                vm.loadDictList();
              } else {
                vm.$notify.error({title: '操作失败', message: ret.msg});
              }
            });
          } else {
            return false
          }
        })
      }, closeDialog(){
        this.$emit('closeDialog');
      }, loadData(){
        var vm = this;
        vm.$parent.loadData(function (ret) {
          if (ret != undefined) {
            if(ret.parentId==undefined){
                ret.parentId=null;
            }
            vm.form = ret;
//            vm.form.menuType=(vm.form.lvl==4)?1:0;
          } else {
            vm.form = Object.assign({}, model);
          }
          if (vm.form.id == undefined || vm.form.id == '') {
            vm.addBtnTxt = '立即创建';
            vm.title = '字典创建';
          } else {
            vm.addBtnTxt = '保存修改';
            vm.title = "字典修改";
          }
        });
      },loadDictList(){
//        var vm=this;
//        http.postData(vm,"/sys/dict/list",{dict_type_id:vm.$parent.form.dict_type_id},function(resp){
//          vm.dictList=resp.body.value;
//        });
      }
    }, created() {
      var vm = this;
//      vm.loadDictList();
    }
  }
</script>
